package base;

import android.content.Context;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;

import java.util.Timer;
import java.util.TimerTask;

public class InfiniteViewPager extends ViewPager {
    private static final int SCROLL_INTERVAL = 3000;

    /**
     * 无限滚动时，该值为页面显示的数量加2
     */
    private int mCount;

    public boolean mIsInfinite = false;

    Timer timer = new Timer();
    MyHandler mHandler = new MyHandler(this);
    private GestureDetector gestureDetector;

    private static class MyHandler extends BFHandler<InfiniteViewPager> {

        private MyHandler(InfiniteViewPager target) {
            super(target);
        }

        @Override
        public void handle(InfiniteViewPager infiniteViewPager, Message message) {
            int newPosition = infiniteViewPager.getCurrentItem() + 1;
            if (newPosition < infiniteViewPager.mCount) {
                infiniteViewPager.setCurrentItem(newPosition);
            }
        }
    }

    public InfiniteViewPager(Context context) {
        this(context, null);
    }

    public InfiniteViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setAdapter(PagerAdapter adapter) {
        if (adapter.getCount() > 1) {
            InfinitePagerAdapter infiniteAdapter = new InfinitePagerAdapter(adapter);
            mCount = infiniteAdapter.getCount();
            super.setAdapter(infiniteAdapter);
        } else {
            mCount = adapter.getCount();
            super.setAdapter(adapter);
        }

        removeOnPageChangeListener(mInfiniteImpl);
        if (mCount > 1) {
            mIsInfinite = true;
            setCurrentItem(1, false);
            addOnPageChangeListener(mInfiniteImpl);
        }
    }

    OnPageChangeListener mInfiniteImpl = new OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_IDLE) {
                if (getCurrentItem() == 0) {
                    setCurrentItem(mCount - 2, false);
                } else if (getCurrentItem() == mCount - 1) {
                    setCurrentItem(1, false);
                }
            }
        }
    };

    /**
     * 设置自动滚动
     */
    public void setAutoScroll(boolean autoScroll) {
        removeOnPageChangeListener(mAutoScrollImpl);
        resetTimer();
        if (autoScroll) {
            addOnPageChangeListener(mAutoScrollImpl);
            timer.schedule(new AutoScrollTask(), SCROLL_INTERVAL, SCROLL_INTERVAL);
        }
    }

    class AutoScrollTask extends TimerTask {

        @Override
        public void run() {
            mHandler.sendEmptyMessage(1);
        }
    }

    OnPageChangeListener mAutoScrollImpl = new OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            resetTimer();
            timer.schedule(new AutoScrollTask(), SCROLL_INTERVAL, SCROLL_INTERVAL);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    private void resetTimer() {
        timer.cancel();
        try {
            timer = new Timer();
        } catch (OutOfMemoryError error) {
            Log.e("test", InfiniteViewPager.class.getSimpleName() + "\n" + error.getMessage());
        }

    }

}
